package sdk.webview.fmc.com.fmcsdk.check;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.Gravity;

public class AutoAlignTextView extends AppCompatTextView {

    public AutoAlignTextView(Context context) {
        super(context);
    }

    public AutoAlignTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoAlignTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 检查是否单行
        Layout layout = getLayout();
        if (layout != null && layout.getLineCount() == 1) {
            setGravity(Gravity.END | Gravity.CENTER_VERTICAL); // 单行靠右
        } else {
            setGravity(Gravity.START | Gravity.CENTER_VERTICAL); // 多行靠左
        }
    }
}
